#ifndef ZHY_MUDUO_BASE_ENDIANZHY_H
#define ZHY_MUDUO_BASE_ENDIANZHY_H

#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <assert.h>

#if defined(_MSC_VER)
#include <WinSock2.h>
#elif defined(__GNUC__)
#include <arpa/inet.h>
#endif

#if 0
const int endian = 1;
#define is_bigendian() ( *(static_cast<char*>(&endian)) == 0 )
#define is_littlendbian() ( *(static_cast<char*>(&endian)) == 1 )

inline uint64_t hostToNetwork64(uint64_t host64)
{
	if (is_littlendbian()) {
		return (uint64_t)(htonl(static_cast<uint32_t>((host64 << 32) >> 32) << 32 | static_cast<uint32_t>(host64 >> 32)));
	} else if (is_bigendian()) {
		return host64;
	} else {
		printf("Unkown Endian\n");
		assert(false);
	}
}

inline uint64_t networkToHost64(uint64_t net64)
{
	//TODO check
	if (is_littlendbian()) {
		return (uint64_t)(htonl(static_cast<uint32_t>((net64 << 32) >> 32)) << 32 
			| (static_cast<uint32_t>(net64 >> 32)));
	} else if (is_bigendian()) {
		return net64;
	} else {
		printf("Unkown Endian\n");
		assert(false);
	}
}
#endif

namespace _zhy_muduo{

#if defined(_MSC_VER)

	inline uint64_t hostToNetwork64(uint64_t host64)
	{
		return htonll(host64);
	}

	inline uint32_t hostToNetwork32(uint32_t host32)
	{
	  return htonl(host32);
	}

	inline uint16_t hostToNetwork16(uint16_t host16)
	{
	  return htons(host16);
	}

	inline uint64_t networkToHost64(uint64_t net64)
	{
		return ntohll(net64);
	}

	inline uint32_t networkToHost32(uint32_t net32)
	{
	  return ntohl(net32);
	}

	inline uint16_t networkToHost16(uint16_t net16)
	{
	  return ntohs(net16);
	}
#elif defined(__GNUC__)
//FIXME: 
inline uint64_t hostToNetwork64(uint64_t host64)
{
	printf("not implement\n");
	abort();
}
inline uint64_t networkToHost64(uint64_t net64)
{
	printf("not implement\n");
	abort();
}

	inline uint32_t hostToNetwork32(uint32_t host32)
	{
	  return htonl(host32);
	}

	inline uint16_t hostToNetwork16(uint16_t host16)
	{
	  return htons(host16);
	}

	inline uint32_t networkToHost32(uint32_t net32)
	{
	  return ntohl(net32);
	}

	inline uint16_t networkToHost16(uint16_t net16)
	{
	  return ntohs(net16);
	}

#else

	static_assert(false, "No instantiation, CHECK SYS!");
#endif
}//namespace _zhy_muduo


#endif //ZHY_MUDUO_BASE_ENDIAN_H
